<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>淘贝商城 - 收藏夹</title>
    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <style>
        .navbar-default {
            background-color: #FFA500; /* 橙黄色 */
            border-color: #E69500;
        }
        .navbar-default .navbar-brand,
        .navbar-default .navbar-nav > li > a {
            color: #FFF; /* 白色文字 */
        }
        .navbar-default .navbar-nav > .active > a,
        .navbar-default .navbar-nav > .active > a:hover,
        .navbar-default .navbar-nav > .active > a:focus {
            background-color: #E69500; /* 深一点的橙黄色 */
        }
    </style>
</head>
<body>
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <div class="navbar-header">
                <a class="navbar-brand" href="#">淘贝商城</a>
            </div>
        </div>
    </nav>

    <div class="container">
        <h1>收藏夹</h1>
        <div class="row" id="favorites-items">
            <!-- 收藏的商品动态渲染 -->
        </div>
        <a href="../index.html" class="btn btn-link">返回主页</a>
    </div>

    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
    <script>
        // 渲染收藏商品
        document.addEventListener('DOMContentLoaded', function () {
            const favorites = JSON.parse(localStorage.getItem('favorites')) || [];
            const favoritesContainer = document.getElementById('favorites-items');
            favoritesContainer.innerHTML = favorites.map(item => `
                <div class="col-md-4">
                    <div class="thumbnail">
                        <img src="${item.image}" alt="${item.name}">
                        <div class="caption">
                            <h3>${item.name}</h3>
                            <p>价格: ￥${item.price}</p>
                            <p>
                                <button class="btn btn-default add-to-cart-from-favorites" data-name="${item.name}" data-price="${item.price}" data-image="${item.image}">添加到购物车</button>
                                <button class="btn btn-danger remove-from-favorites" data-name="${item.name}">取消收藏</button>
                            </p>
                        </div>
                    </div>
                </div>
            `).join('');

            // 添加到购物车功能
            const addToCartButtons = document.querySelectorAll('.add-to-cart-from-favorites');
            addToCartButtons.forEach(button => {
                button.addEventListener('click', function () {
                    const product = {
                        name: button.getAttribute('data-name'),
                        price: button.getAttribute('data-price'),
                        image: button.getAttribute('data-image')
                    };
                    addToCart(product);
                });
            });

            // 取消收藏功能
            const removeButtons = document.querySelectorAll('.remove-from-favorites');
            removeButtons.forEach(button => {
                button.addEventListener('click', function () {
                    const itemName = button.getAttribute('data-name');
                    removeFromFavorites(itemName);
                    location.reload(); // 刷新页面
                });
            });

            function addToCart(product) {
                let cart = JSON.parse(localStorage.getItem('cart')) || [];
                cart.push(product);
                localStorage.setItem('cart', JSON.stringify(cart));
                alert('商品已添加到购物车！');
            }

            function removeFromFavorites(name) {
                let favorites = JSON.parse(localStorage.getItem('favorites')) || [];
                favorites = favorites.filter(item => item.name !== name);
                localStorage.setItem('favorites', JSON.stringify(favorites));
                alert('商品已取消收藏！');
            }
        });
    </script>
</body>
</html>